package com.jiulan.hearthearing.ui.pay.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.my.activity.CoursesPurchasedActivity;
import com.jiulan.hearthearing.ui.pay.adapter.SettlementDeskAdapter;
import com.jiulan.hearthearing.ui.pay.model.OrderConfirmModel;
import com.jiulan.hearthearing.ui.pay.model.OrderCreateModel;

import java.util.HashMap;
import java.util.Map;

/**
 * 结算台
 */
public class SettlementDeskActivity extends BaseActivity implements View.OnClickListener {
    private String cartId;
    private OrderConfirmModel orderConfirmModel;
    private SettlementDeskAdapter settlementDeskAdapter;

    private ImageView _barBack;
    private TextView _barTitle;
    private RecyclerView mRecyclerView;
    private TextView tvYue;
    private TextView tvChongzhi;
    private ImageView ivIsUseYue;
    private ImageView ivIsUseWeixin;
    private ImageView ivIsUseZhifubao;
    private TextView tvYingfu;
    private TextView tvYouhui;
    private TextView tvConfirmPay;

    public static void actionStart(Context context, String cartId) {
        Intent intent = new Intent(context, SettlementDeskActivity.class);
        intent.putExtra("cartId", cartId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_settlement_desk;
    }

    @Override
    protected void initView() {
        cartId = getIntent().getStringExtra("cartId");
        _barBack = findViewById(R.id._barBack);
        _barTitle = findViewById(R.id._barTitle);
        _barTitle.setText("结算台");
        mRecyclerView = findViewById(R.id.mRecyclerView);
        tvYue = findViewById(R.id.tvYue);
        tvChongzhi = findViewById(R.id.tvChongzhi);
        ivIsUseYue = findViewById(R.id.ivIsUseYue);
        ivIsUseWeixin = findViewById(R.id.ivIsUseWeixin);
        ivIsUseZhifubao = findViewById(R.id.ivIsUseZhifubao);
        tvYingfu = findViewById(R.id.tvYingfu);
        tvYouhui = findViewById(R.id.tvYouhui);
        tvConfirmPay = findViewById(R.id.tvConfirmPay);
        _barBack.setOnClickListener(this);
        tvChongzhi.setOnClickListener(this);
        ivIsUseYue.setOnClickListener(this);
        ivIsUseWeixin.setOnClickListener(this);
        ivIsUseZhifubao.setOnClickListener(this);
        tvConfirmPay.setOnClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        getData();
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("cartId", cartId);
        new HttpRequest(getContext()).doPost(UrlConstants.order_confirm, "", map, OrderConfirmModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof OrderConfirmModel) {
                    orderConfirmModel = (OrderConfirmModel) result;
                    setView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setView() {
        settlementDeskAdapter = new SettlementDeskAdapter(orderConfirmModel.getData().getCartInfo());
        mRecyclerView.setAdapter(settlementDeskAdapter);
        tvYue.setText("¥" + orderConfirmModel.getData().getUserInfo().getNow_money());
        tvYingfu.setText("¥" + orderConfirmModel.getData().getPriceGroup().getTotalPrice());
        tvYouhui.setText("¥" + orderConfirmModel.getData().getPriceGroup().getCostPrice());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id._barBack:
                finish();
                break;
            case R.id.ivIsUseCoupon:
                break;
            case R.id.tvChongzhi:
                break;
            case R.id.ivIsUseYue:
                break;
            case R.id.ivIsUseWeixin:
                break;
            case R.id.ivIsUseZhifubao:
                break;
            case R.id.tvConfirmPay:
                pay(orderConfirmModel.getData().getOrderKey());
                break;
        }
    }

    /**
     * 支付
     */
    private void pay(String orderKey) {
        Map<String, String> map = new HashMap<>();
        map.put("real_name", orderConfirmModel.getData().getUserInfo().getReal_name());
        map.put("phone", orderConfirmModel.getData().getUserInfo().getPhone());
        map.put("addressId", orderConfirmModel.getData().getUserInfo().getAddres());
        map.put("useIntegral", orderConfirmModel.getData().getUserInfo().getIntegral());
        map.put("couponId", "");
        map.put("payType", "yue");
        map.put("pinkId", "");
        map.put("seckill_id", "");
        map.put("combinationId", "");
        map.put("bargainId", "");
        map.put("from", "app");
        map.put("mark", "");
        map.put("shipping_type", "1");
        new HttpRequest(getContext()).doPost(UrlConstants.order_create + orderKey, "", map, OrderCreateModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof OrderCreateModel) {
                    OrderCreateModel orderCreateModel = (OrderCreateModel) result;
                    Toast.makeText(getContext(), orderCreateModel.getMsg(), Toast.LENGTH_SHORT).show();
                    CoursesPurchasedActivity.actionStart(getContext());
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }
}